package cn.coisini.controller.v1;

import cn.coisini.api.admin.AuthenticationControllerApi;
import cn.coisini.model.common.annotation.RedisAccessLimit;
import cn.coisini.model.common.dtos.Result;
import cn.coisini.model.common.enums.ResultEnum;
import cn.coisini.model.common.enums.SmsType;
import cn.coisini.model.common.log.annotation.Log;
import cn.coisini.model.common.log.enums.BusinessType;
import cn.coisini.model.dto.UserDto;
import cn.coisini.security.auth.AuthUser;
import cn.coisini.service.AuthService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

/**
 * @author xiangshaw
 * Description:
 */
@RestController
@RequiredArgsConstructor
public class AuthenticationController implements AuthenticationControllerApi {

    private final AuthService authService;

    // 用户信息查询
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public  Result<Map<String, Object>>  userInfo() {
        return authService.getUserInfo();
    }

    // 用户注册
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户认证管理", businessType = BusinessType.INSERT)
    public Result<ResultEnum> register(@RequestBody UserDto userDto) {
        return authService.register(userDto);
    }

    // 用户登录认证
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<String>  authenticate(@RequestBody AuthUser authUser, HttpServletRequest request) throws IOException {
        return authService.authenticate(authUser,request);
    }

    // 获取验证码
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<Map<String, Object>> captcha() throws IOException {
        return authService.captcha();
    }

    // 发送验证码
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 1)
    public Result<ResultEnum> sendSms(String mode, SmsType smsType) {
        return authService.sendSms(mode, smsType);
    }

    // 用户退出登录
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "用户认证管理", businessType = BusinessType.LOGOUT)
    public Result<ResultEnum> logout() {
        return authService.logout();
    }

    // are you ok
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<String> hello() {
        return authService.hello();
    }

}
